package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author wgq
 * @date 2019-10-14 21:02
 * 预约设置，文件上传服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 将上传文件里的预约设置，保存添加到数据库中
     * @param orderSettingList
     */
    @Override
    public void addToDb(List<OrderSetting> orderSettingList) {
        if(orderSettingList != null && orderSettingList.size()>0){
            //遍历list集合，对每一条内容进行判断处理
            for (OrderSetting orderSetting : orderSettingList) {
                //检查此日期是否存在
                Long count = orderSettingDao.findCountByOrderDate(orderSetting);
                if(count>0){
                    //如果已经存在 ，执行更新操作
                    orderSettingDao.editOrderSetting(orderSetting);
                }else{
                    //如果不存在，执行添加操作
                    orderSettingDao.addOrderSetting(orderSetting);
                }
            }

        }
    }

    /**
     * 通过月份，获取预约设置信息，显示到前端页面上
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        /*Map map = new HashMap();
        //这里是SQL中用了Between .... and ...的方法进行了查询当前月份
        //select * from t_ordersetting where orderDate between #{dateBegin} and #{dateEnd}
        map.put("dateBegin",date+"-1");
        map.put("dateEnd",date+"-31");
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);*/
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(date);
        List<Map> data = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        for (OrderSetting orderSetting : orderSettingList) {
            Map orderSetttingMap = new HashMap();
            c.setTime(orderSetting.getOrderDate());
            orderSetttingMap.put("date",c.get(Calendar.DAY_OF_MONTH));
            orderSetttingMap.put("number",orderSetting.getNumber());
            orderSetttingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSetttingMap);
        }
        System.out.println("data:"+data);
        //[{"orderDate":"2019/12/12,"number":300,"reservations":0},{"orderDate":"2019/11/11,"number":500,"reservations":10}]
        return data;
    }

    /**
     * 前端界面进行预约设置， 设置可预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        if(orderSettingDao.findCountByOrderDate(orderSetting) >0){
            orderSettingDao.editOrderSetting(orderSetting);
        }else{
            orderSettingDao.addOrderSetting(orderSetting);
        }

    }
}
